﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManage
{
    public class Order
    {
        public int OrderId { get; set; }
        public string Customer { get; set; }
        public readonly HashSet<OrderDetails> _details = new();
        public IEnumerable<OrderDetails> Details =>
            _details;

        public decimal TotalAmount =>
            _details.Sum(d => d.Quantity * d.Price);

        //添加订单明细
        public void AddDetail(OrderDetails detail)
        {
            if (!_details.Add(detail))
            {
                throw new InvalidOperationException("订单明细已存在");
            };
        }

        public override bool Equals(object? obj) =>
            obj is Order order &&
            OrderId == order.OrderId;

        public override int GetHashCode() =>
            OrderId.GetHashCode();

        //格式化输出订单信息
        public override string ToString() =>
            $"订单号：{OrderId}, 客户：{Customer}, 总金额：{TotalAmount:C2}\n"
            + "订单明细:\n" + string.Join("\n", Details);
    }
}
